import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.15
import mytablemodel 1.0

Window {
    id:rectangle
    visible: true
    width: 800
    height: 480
    title: qsTr("Hello World")
    color: "#0A3653"
    property int selectedView: 0
    property bool screenSaverActive: false
//    property string m_startTime:Qt.formatDateTime(new Date(), "yyyy-MM-dd 00:00:00")
//    property string m_endTime:Qt.formatDateTime(new Date(), "yyyy-MM-dd 23:59:59")
//    property int m_currentPage:0

    // 重置屏幕保护定时器的函数
    function resetScreenSaverTimer() {
        if (screenSaverTimer.running) {
            if(mymodbus.screenSaveTime===0)
            {
                screenSaverTimer.stop()
            }
            else
            {
                screenSaverTimer.interval = mymodbus.screenSaveTime*1000
                screenSaverTimer.restart()
            }
        }
        else{
            if(mymodbus.screenSaveTime!==0)
            {
                screenSaverTimer.interval = mymodbus.screenSaveTime*1000
                screenSaverTimer.start()            
            }
        }
        
        // 如果屏幕保护已激活，则取消激活状态
        if (screenSaverActive) {
            screenSaverActive = false
        }
    }

    function stopScreenSaverTimer() {
        screenSaverTimer.stop()
        // 如果屏幕保护已激活，则取消激活状态
        if (screenSaverActive) {
            screenSaverActive = false
        }
    }
    
    Component.onCompleted: {
        myLoader.source="StatusView.qml"
        if(mymodbus.screenSaveTime!==0)
        {
            screenSaverTimer.start()  // 启动屏幕保护定时器
        }
    }
    // 屏幕保护定时器
    Timer {
        id: screenSaverTimer
        interval: mymodbus.screenSaveTime*1000
        repeat: false
        onTriggered: {
            // 关闭背光
            mymodbus.setbacklightvalue(0)
            screenSaverActive = true
            fullScreenButton.visible = true
            fullScreenButton.enabled = true            
        }
    }
    // 全屏按钮 - 用于恢复屏幕
    Button {
        id: fullScreenButton
        anchors.fill: parent
        visible: false
        enabled: false
        z: 10000  // 确保在最上层
        
        background: Rectangle {
            color: "transparent"
        }
        
        contentItem: Rectangle {
            anchors.fill: parent
            //color: "#80000000"  // 半透明黑色背景
            color: "#000000"  // 修改为不透明黑色
            Text {
                anchors.centerIn: parent
                text: ""
                font.pixelSize: 32
                color: "white"
            }
        }
        
        onClicked: {
            // 隐藏按钮
            fullScreenButton.visible = false
            fullScreenButton.enabled = false
            screenSaverActive = false
            // 重新启动屏幕保护定时器
            if(mymodbus.screenSaveTime===0)
            {
                screenSaverTimer.stop()
            }
            else
            {
                screenSaverTimer.interval = mymodbus.screenSaveTime*1000
                screenSaverTimer.restart()
            }
            // 恢复背光
            mymodbus.setbacklightvalue(mymodbus.backlight)
        }
    }

    // 全局鼠标区域，用于检测屏幕点击
    MouseArea {
        id: globalMouseArea
        anchors.fill: parent
        propagateComposedEvents: true  // 允许事件继续传递
        z: 9999  // 确保在全屏按钮下方
        
        onClicked: {
            // 只有当全屏按钮不可见时才重置定时器
            //console.log("onClicked");
            if (!fullScreenButton.visible) {
                resetScreenSaverTimer()
            }
            mymodbus.beep(50)
            mouse.accepted = false  // 允许事件继续传递到下层组件
        }
        
        onPressed: {
            // 只有当全屏按钮不可见时才重置定时器
            //console.log("onPressed");
            if (!fullScreenButton.visible) {
                resetScreenSaverTimer()
            }
            mymodbus.beep(50)
            mouse.accepted = false
        }
    }

    // 页面加载时也重置定时器
    function onPageLoaded() {
        resetScreenSaverTimer()
    }
    Item {
        id: mainItem_
        anchors.fill:parent
        //visible: false
        TopBar {
            id: topBar
            width: parent.width
            height: 29
        }
        Item {
            id: mainItem
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.top: topBar.bottom
            height: parent.height - topBar.height - bottomBar.height

            property int currentViewIndex: 0
            Loader {
                anchors.fill:parent
                id: myLoader
                asynchronous: true
                visible:true
                onSourceChanged: {
                    loaderText.visible = false
                    // 页面切换时重置屏幕保护定时器
                    resetScreenSaverTimer()
                }
                //source: "StatusView.qml"
               //sourceComponent: rect
//                onVisibleChanged: {
//                    if(!visible)
//                    {
//                        loaderText.visible = false
//                        if(myLoader.item)
//                        {
//                            myLoader.item.destroy()
//                        }
//                    }
//                }
                onLoaded: {
                    // 页面加载完成时重置屏幕保护定时器
                    resetScreenSaverTimer()
                }
            }

//            StatusView {
//                id: status
//                enabled: rectangle.selectedView === 0
//                visible: opacity > 0.01
//                anchors.fill: parent
//                selectedView: rectangle.selectedView
//                index: 0
//            }

//            DataView {
//                id: dataPanel
//                enabled: rectangle.selectedView === 1
//                visible: opacity > 0.01
//                anchors.fill: parent
//                selectedView: rectangle.selectedView
//                index: 1
//            }

//            FunctionPanel {
//                id: functionPanel
//                enabled: rectangle.selectedView === 2
//                visible: opacity > 0.01
//                anchors.fill: parent
//                selectedView: rectangle.selectedView
//                index: 2
//            }
//            VersionPanel {
//                id: versionPanel
//                enabled: rectangle.selectedView === 3
//                visible: opacity > 0.01
//                anchors.fill: parent
//                selectedView: rectangle.selectedView
//                index: 3
//            }
//            SettingsPanel {
//                id: settingsPanel
//                enabled: rectangle.selectedView === 4
//                visible: opacity > 0.01
//                anchors.fill: parent
//                selectedView: rectangle.selectedView
//                index: 4
//            }
//            ChangePwd {
//                        id: changePwdPanel
//                        enabled: rectangle.selectedView === 5
//                        visible: opacity > 0.01
//                        anchors.fill: parent
//                        selectedView: rectangle.selectedView
//                        index: 5
//                    }
//            ResetPanel {
//                        id: resetPanel
//                        enabled: rectangle.selectedView === 6
//                        visible: opacity > 0.01
//                        anchors.fill: parent
//                        selectedView: rectangle.selectedView
//                        index: 6
//                    }
//            EnterPwdPanel {
//                        id: enterPwdPanel
//                        enabled: rectangle.selectedView === 7
//                        visible: opacity > 0.01
//                        anchors.fill: parent
//                        selectedView: rectangle.selectedView
//                        index: 7
//                    }
//            GeneralSettingsPanel {
//                        id: generalSettingsPanel
//                        enabled: rectangle.selectedView === 8
//                        visible: opacity > 0.01
//                        anchors.fill: parent
//                        selectedView: rectangle.selectedView
//                        index: 8
//                    }
//            HoldModeSettings {
//                       id: holdModeSettingsPanel
//                       enabled: rectangle.selectedView === 9
//                       visible: opacity > 0.01
//                       anchors.fill: parent
//                       selectedView: rectangle.selectedView
//                       index: 9
//                   }
//            PresetPositionSettingPanel {
//                       id: presetPosSettingsPanel
//                       enabled: rectangle.selectedView === 10
//                       visible: opacity > 0.01
//                       anchors.fill: parent
//                       selectedView: rectangle.selectedView
//                       index: 10
//                   }
//            LanguagePanel {
//                       id: languagePanel
//                       enabled: rectangle.selectedView === 11
//                       visible: opacity > 0.01
//                       anchors.fill: parent
//                       selectedView: rectangle.selectedView
//                       index: 11
//                   }
//            ZeroCalibration {
//                       id: zeroCalibrationPanel
//                       enabled: rectangle.selectedView === 12
//                       visible: opacity > 0.01
//                       anchors.fill: parent
//                       selectedView: rectangle.selectedView
//                       index: 12
//                   }
//            GainCalibration {
//                       id: gainCalibrationPanel
//                       enabled: rectangle.selectedView === 13
//                       visible: opacity > 0.01
//                       anchors.fill: parent
//                       selectedView: rectangle.selectedView
//                       index: 13
//                   }
//            NormalSettings {
//                       id: normalSettingsPanel
//                       enabled: rectangle.selectedView === 14
//                       visible: opacity > 0.01
//                       anchors.fill: parent
//                       selectedView: rectangle.selectedView
//                       index: 14
//                   }
//            CommunucationSettings {
//                       id: communicationSettingsPanel
//                       enabled: rectangle.selectedView === 15
//                       visible: opacity > 0.01
//                       anchors.fill: parent
//                       selectedView: rectangle.selectedView
//                       index: 15
//                   }
//            EnterNumberPanel {
//                       id: enterNumberPanel
//                       enabled: rectangle.selectedView === 16
//                       visible: opacity > 0.01
//                       anchors.fill: parent
//                       selectedView: rectangle.selectedView
//                       index: 16
//                   }


        }
//        StatusView {
//            id: statusView
//            visible: true
//            anchors.left: parent.left
//            anchors.right: parent.right
//            anchors.top: topBar.bottom
//            height: parent.height - topBar.height - bottomBar.height
//        }
//        DataView {
//            id: dataPanel
//            visible: false
//            anchors.left: parent.left
//            anchors.right: parent.right
//            anchors.top: topBar.bottom
//            height: parent.height - topBar.height - bottomBar.height
//        }
        BottomBar { //哦哦，是否可以使用QML的TabBar。
            id: bottomBar
            x: 0
            y: 420
            width: 800
            height: 60
            showMainOption: true
//            Connections {
//                target: bottomBar
//                function onViewSwitched(index: int) {
//                    rectangle.selectedView = index
//                }
//            }
        }
//        MyTableModel{
//            id:weightTableModel
//            startTime: m_startTime
//            endTime: m_endTime
//        }

        MyBuzyIndicator{
            anchors.centerIn: parent
            id: mainLoader
            visible:myLoader.visible&&myLoader.status!=Component.Ready
        }
        Text
        {
            id:loaderText
            visible:false
            width:350
            height:150
            anchors.centerIn: parent
            font.pixelSize: 50
            horizontalAlignment: Text.AlignHCenter//(1)
            verticalAlignment: Text.AlignVCenter//(2)
            text: "Processing..."
            color:"white"
            Rectangle{
                anchors.fill:parent
                color: Qt.rgba(10/255, 54/255, 83/255, 0.5)
                z:-1
            }
        }
        Dialog {
            anchors.centerIn: parent
            title:"Failed!"
            modal: true
            id: countPanelDialog
            standardButtons: Dialog.Ok
            closePolicy: Popup.CloseOnEscape
            onAccepted: {
                countPanelDialog.close();
            }
            z:100
        }
        Dialog {
            anchors.centerIn: parent
            title:qsTr("已下载升级包，重启生效！")
            modal: true
            id: otaDownloadDialog
            standardButtons: Dialog.Ok
            closePolicy: Popup.CloseOnEscape
            onAccepted: {
                otaDownloadDialog.close();
            }
            visible: mymodbus.downloadOTAPackage
            z:100
        }
    }
//    MyBuzyIndicator{
//        anchors.centerIn: parent
//        id: initLoader
//        visible:mymodbus.init
//        onVisibleChanged: {
//            if(!visible){
//                myLoader.source="StatusView.qml"
//                mainItem_.visible=true;
//            }
//        }
//    }
}
